<?php
/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <r.mujica.91@gmail.com> wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return 
 * ----------------------------------------------------------------------------
 */

require('includes/smartyass.init.php');

$email = $_POST['email'];
$error = '';

# validates e-mail
if( !preg_match('/^[a-z][\w\.+-]*[a-z0-9]@[a-z0-9][\w\.+-]*\.[a-z][a-z\.]*[a-z]$/i', $email) )
{
  $error = 'Bad email';
}

$account = new OTS_Account($email);

# checks if this e-mail was already used
if($account->loaded)
{
	$error = 'Used email';
}

# all right, now create the account!
# we'll generate the account name
try
{
  $name = $account->createNamed();
}
catch(E_OTS_Generic $e)
{
	# an error has ocurred :(
	$error = 'An error has ocurred during account creation.';
}

# now we add all the info
# randomize a password...
$password = substr( md5( uniqid( rand(), true) ), 1, 8);

// sets all info
$account->blocked = false;

// generates password hash if used
switch($cfg['aaci']['hashing'])
{
    case 'md5':
        $account->password = md5($password);
        break;

    case 'sha1':
        $account->password = sha1($password);
        break;

    case 'plain':
        $account->password = $password;
        break;
}

$account->eMail = $email;
$account->save();

# all fine, lets output now!

$tpl = new Frontend();

# lets see if we have an error
if($error != '')
{
	echo $error;
}
else
{
	echo 'so gut';
	echo $account->password;
}
?>